<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\SysMessage;
use yii\base\Object;

/**
 * SysMessageSearch represents the model behind the search form about `backend\models\SysMessage`.
 */
class SysMessageSearch extends SysMessage
{
    /**
     * @inheritdoc
     */
	public $create_time_1;
	public $status_1;
	public $send_time_1;
	public $type_1;
    public function rules()
    {
        return [
            [['id', 'create_time', 'type', 'icon'], 'integer'],
            [['title', 'content'], 'safe'],
            [['create_time_1','send_time_1','status_1','type_1'], 'string', 'max' => 128],
            ['status_1','in','range'=>['编辑','审核','发送'],'message'=>'请填入编辑|审核|发送'],
            ['type_1','in','range'=>['系统消息','活动消息','聊天消息'],'message'=>'请填入系统消息|活动消息|聊天消息'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = SysMessage::find() ;

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        
        $dataProvider->setSort([
        		'attributes' => [
        		'id',
        		'create_time',
        		'status',
        		'send_time',
        		'type',
        		 'icon',
        		'title',
        		'content',
        		
        		'status_1'=>[
        		'asc' => ['status' =>SORT_ASC],
        		'desc' => ['status' =>SORT_DESC],
        				'label'=> Yii::t('app','Message Status')
        		],
        		'type_1'=>[
        		'asc' => ['type' =>SORT_ASC],
        		'desc' => ['type' =>SORT_DESC],
        				'label'=> Yii::t('app','Message Type')
        		],
        		
        		'create_time_1'=>[
        		'asc' => ['create_time' =>SORT_ASC],
        		'desc' => ['create_time' =>SORT_DESC],
        		'label'=> Yii::t('app','Create Time')
        		],
        		'send_time_1'=>[
        		'asc' => ['send_time' =>SORT_ASC],
        		'desc' => ['send_time' =>SORT_DESC],
        		'label'=> Yii::t('app','Message Send Time')
        		],
        		 
        		],
        		]);
        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'create_time' => $this->create_time,
            'type' => $this->type,
            'icon' => $this->icon,
        ]);

        $query->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'content', $this->content])
            ->andFilterWhere(['like','type',$this->setType($this->type_1)])
           	->andFilterWhere(['like','status',$this->setStatus($this->status_1)])
           
            ->andFilterWhere(['like','FROM_UNIXTIME(create_time, "%Y-%m-%d %H:%i:%S")',$this->create_time_1])
        	->andFilterWhere(['like','FROM_UNIXTIME(send_time, "%Y-%m-%d %H:%i:%S")',$this->send_time_1]);

        return $dataProvider;
    }
    public function setType($string)
    {
        $_arr = [
	        ''=>'',
	        '未设置'=>'0',
	        '系统消息'=>'1',
	        '活动消息'=>'2',
	        '聊天消息'=>'3'     
        ];
    	return $_arr[$string];
    }
    public function setStatus($string)
    {
		$_arr = [
			''=>'',
			'编辑'=>'1',
			'审核'=>'2',
			'发送'=>'3'
				];	
    	return $_arr[$string];
    }
	public static function checkStatus($status)
	{
		if($status == '3')return '{view}';
	}
}
